<!--
Date: 2025-08-13
Copyright (c) 2025 Industrial Software Feature Database
-->
<template>
  <div class="public_main">
    <div class="query">
      <el-form inline ref="queryFormRef" :model="queryParams" label-width="100px" size="small">
        <el-form-item label="机型:">
          <el-select v-model="queryParams.modelNumber" placeholder="请选择机型">
            <el-option v-for="item in modelNumberList" :label="item.name" :value="item.mark" :key="item.mark"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="组件名称:">
          <el-input v-model="queryParams.fileName" placeholder="请输入组件名称" />
        </el-form-item> -->
        <el-form-item class="query_btns">
          <el-button size="small" type="primary" icon="search" @click="doQuery">查询</el-button>
          <el-button size="small" type="default" icon="refresh" @click="doReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table_div">
      <el-button size="small" type="primary" class="upload_btn" plain icon="Plus" @click="handleVisible('add')">新增</el-button>
      <el-button size="small" type="danger" class="upload_btn" plain icon="Delete" @click="handleDelete()">删除</el-button>
      <el-table :data="tableData" v-loading="loading" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="边界条件文件上传日期" align="center" prop="createTime" fixed="left"> </el-table-column>
        <el-table-column label="文件名称" align="center" prop="fileName"> </el-table-column>
        <el-table-column label="文件大小（KB）" align="center" prop="fileSize"> </el-table-column>
        <el-table-column label="所属机型" align="center" prop="modelNumber"> </el-table-column>
        <el-table-column label="所属批次" align="center" prop="batchName"> </el-table-column>
        <el-table-column label="备注" align="center" prop="remarks"> </el-table-column>
        <el-table-column label="操作" align="center" width="250">
          <template #default="scope">
            <el-button size="small" link type="primary" icon="View" @click="view(scope.row)">预览</el-button>
            <el-button size="small" link type="primary" icon="View" @click="copy(scope.row)">复制到新批次</el-button>
            <el-button size="small" link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagenation">
        <el-pagination
          v-show="total > 0"
          layout="prev, pager, next,jumper"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
  <AddBoundary :addVisible="addVisible" :modelList="modelList" :initBoundaryData="initBoundaryData" v-if="addVisible" @handleEvent="handleEvent" />
  <BdfPreView v-if="preViewVisible" :data="preViewParams" :preViewVisible="preViewVisible" @handleEvent="handleView" />
</template>
<script setup>
import { ref, onMounted } from 'vue';
import AddBoundary from './components/AddBoundary.vue';
import BdfPreView from '../util/BdfPreView.vue';
import { getStructureTreeList } from '@/api/elementsLibrary';
import { getBoundaryOptionList, deleteBoundaryOption, deleteBoundaryOptionValidate, getElementLibraryFile } from '@/api/elementLibrary';
const { proxy } = getCurrentInstance();
const tableData = ref([]);
const ids = ref([]);
const addVisible = ref(false);
const modelList = ref([]);
const modelNumberList = ref([]);
const preViewVisible = ref(false);
const preViewParams = ref();
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  modelNumber: '',
  orderByColumn: 'createTime',
  isAsc: 'descending'
});
const initBoundaryData = ref({
  id: '',
  type: '',
  modelNumber: '',
  batchNumber: '',
})
const total = ref(0);
const uploadOp2Visible = ref(false);
const currentRow = ref(null);
const resultList = ref([]);
const loading = ref(false);

const handleCurrentChange = (number) => {
  queryParams.value.pageNum = number;
  doQuery();
};
const handleView = () => {
  preViewVisible.value = false;
};
// 预览
const view = (row) => {
  const { fileUrl, fileName } = row;
  preViewParams.value = { classification: 'model_management_boundary_data_prefix', uri: fileUrl, fileName: fileName };
  preViewVisible.value = true;
};
// 复制到新批次
const copy = (row) => {          
  initBoundaryData.value.id = row.id;
  initBoundaryData.value.type = 'copy';
  initBoundaryData.value.modelNumber = row.modelNumber;    
  initBoundaryData.value.batchNumber = row.batchNumber;
  initBoundaryData.value.remarks = row.remarks;
  addVisible.value = true;
}
//查询数据列表
const doQuery = () => {
  loading.value = true;
  tableData.value = [];
  getBoundaryOptionList(queryParams.value)
    .then((res) => {
      // 处理数据
      loading.value = false;
      total.value = res?.total;
      tableData.value = res.rows;
    })
    .catch(() => {
      loading.value = false;
    });
};
const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id);
};
// 回调函数
const handleEvent = (data) => {
  addVisible.value = false;
  if (data?.type === 'refresh') {
    doQuery();
  }
};
const doReset = () => {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    modelNumber: '',
    orderByColumn: 'createTime',
    isAsc: 'descending'
  };
  doQuery();
};
// 打开弹窗方法
const handleVisible = (param, data) => {
  if (param == 'add') {
    initBoundaryData.value.id = '';
    initBoundaryData.value.type = 'add';
    initBoundaryData.value.modelNumber = '';    
    initBoundaryData.value.batchNumber = '';
    initBoundaryData.value.remarks = '';
    addVisible.value = true;
  }
};
//删除
const handleDelete = (row) => {
  const dataIds = row?.id ? [row?.id] : ids.value;
  deleteBoundaryOptionValidate(dataIds).then(async res => {
    let infoMsg = !res.data ? res.msg : (row?.id ? '是否确认删除当前数据项？' : '是否确认删除当前所选全部数据项？')
    ElMessageBox.confirm(infoMsg, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      deleteBoundaryOption(dataIds)
      .then((res) => {
        if (res) {
          ElMessage({ showClose: true, message: '删除成功。', type: 'success' });
          doQuery();
        }
      })
      .catch(() => {
        ElMessage({ showClose: true, message: '删除失败。', type: 'error' });
      });
    });
  })
};
const getModelList = () => {
  getStructureTreeList({pageSize: 9999}).then(res => {
    modelList.value = res.rows;
    modelNumberList.value = res?.rows?.filter((el) => {
      return el.parentId === 0;
    });
  })
}
onMounted(() => {
  doQuery();
  getModelList();
});
</script>
<style lang="scss" scoped>
.public_main {
  .query {
    border: 1px solid #dddddd7a;
    padding: 20px 10px 0 10px;
    .query_btns {
      padding-left: 100px;
    }
  }
  .table_div {
    margin-top: 10px;
    border: 1px solid #dddddd7a;
    padding-bottom: 10px;
    .upload_btn {
      margin: 10px 10px;
    }
    .pagenation {
      display: flex;
      justify-content: end;
      margin-top: 10px;
    }
  }
  :deep(.el-form-item__content) {
    width: 220px;
  }
  :deep(.el-tree-node__label) {
    font-size: 12px;
    color: #606266;
  }
}
</style>
      